/*
*APCED - Data preparation for community cohort's multi-treatment propensity score analysis
*Hyesung "Hace" Oh
*Started 11/07/2021
*Last worked on: 03/14/2023
*The purpose of this .do file is to generate Stata functions for use in the analysis
********************************************************************************
*/

*Table 1 generator function
capture program drop table1by_gen
program define table1by_gen
syntax varlist [using/] [if], by(string) [mean_dec_place(int 2)] [sd_dec_place(int 2)] [replace]
	
preserve

	capture keep `if'
	
	keep `varlist' `by'
	
	if "`using'" == "" {
	disp "This is a descriptive statistics table by `by' `if' saved to the default name, table1_by"
	post_out `varlist' `if' using table1_by, by(`by') `replace'
	use table1_by, clear
	clean_out, by(`by') mean_dec_place(`mean_dec_place') sd_dec_place(`sd_dec_place')
	export delimited using table1_by, replace 
	erase table1_by.dta
	}
	
	else {
	disp "This is a descriptive statistics table by `by' `if', saved to  `using'"
	post_out `varlist' `if' using `using', by(`by') `replace'
	use "`using'", clear
	clean_out, by(`by') mean_dec_place(`mean_dec_place') sd_dec_place(`sd_dec_place')
	export delimited using `using', replace
	erase "`using'.dta"
	}
	
restore
end

capture program drop post_out
program define post_out
syntax varlist [using/] [if], by(string) [replace]

quietly {
	capture drop `by'_group
	egen `by'_group = group(`by')
	quietly sum `by'_group
	local bymin = `r(min)'
	local bymax = `r(max)'
	
	local variable_mean_header_list  		
	forvalues num = `bymin'/`bymax' {
		local variable_mean_header_list "`variable_mean_header_list' mean_`by'_`num'"
		local variable_mean_header_list "`variable_mean_header_list' sd_`by'_`num'"	
	} 
	if "`using'" == "" {
		local table_name "table1_by"
	}
	else {
		local table_name  `using'
	}
		capture postclose thetable
		postfile thetable str80 variable s_mean s_sd `variable_mean_header_list' using "`table_name'", `replace'
		foreach var of varlist `varlist' {
			local `var'_by_list //start empty list of values
			capture quietly sum `var' `if'
			local `var'_by_list "``var'_by_list' (`r(mean)') (`r(sd)')"
				forvalues num = `bymin'/`bymax' {
				sum `var' if `by'_group == `num'
				local `var'_by_list "``var'_by_list' (`r(mean)') (`r(sd)')"	
				}
		post thetable ("`var'") ``var'_by_list'
		}
		postclose thetable		
}
end

capture program drop clean_out
program define clean_out
syntax, by(string) [mean_dec_place(int 2)] [sd_dec_place(int 2)] 
quietly {
	describe
	local grmin = 1
	local grmax = (`r(k)'/2) - `grmin'
	local variable_mean_header_list  //start empty list
			gen open_brace = " ("
			gen close_brace = ")"
			gen entire_sample_mean_rd_str = string(s_mean, "%04.`mean_dec_place'f")
			gen entire_sample_sd_rd_str = string(s_sd, "%04.`sd_dec_place'f")
			egen mean_`by'_entire_open = concat(entire_sample_mean_rd_str open_brace)
			egen sd_`by'_entire_close = concat(entire_sample_sd_rd_str close_brace)
			egen mean_sd_`by'_entire = concat(mean_`by'_entire_open sd_`by'_entire_close)
			
			forvalues num = `grmin'/`grmax' {
				gen mean_`by'_`num'_rd_str = string(mean_`by'_`num', "%04.`mean_dec_place'f")		
				gen sd_`by'_`num'_rd_str = string(sd_`by'_`num', "%04.`sd_dec_place'f")
				egen mean_`by'_`num'_open = concat(mean_`by'_`num'_rd_str open_brace)
				egen sd_`by'_`num'_close = concat(sd_`by'_`num'_rd_str close_brace)
				egen mean_sd_`by'_`num' = concat(mean_`by'_`num'_open sd_`by'_`num'_close)
			}
		keep variable mean_sd_`by'*
}
	list, clean noobs		
end
****************************************

*Function that squares specified variables*
capture program drop square_vars
program define square_vars
syntax varlist
	foreach v in `varlist' {
		capture drop `v'2
		gen `v'2 = `v'^2
	}

end


*Function that generates interaction terms*
capture program drop interact_vars
program define interact_vars
syntax, list1(string) list2(string)
	foreach v in `list1' {
		foreach u in `list2' {
			capture drop `v'_`u'
			gen `v'_`u' = `v'*`u' 
		}
	}

end


*Function that generates a multinomial p-score*
capture program drop p_score_gen
program define p_score_gen
syntax varname, covars(string) [robust] [cluster(string)]
	capture drop `varlist'_p_score*
	quietly mlogit `varlist' `covars', `robust' `cluster'
	predict `varlist'_p_score*
	sum `varlist'_p_score*

end